package top.go2do.deviceSync.business.device.controller;

import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.go2do.deviceSync.business.device.module.DeviceHeartbeatBO;
import top.go2do.deviceSync.business.device.module.DeviceHeartbeatDTO;
import top.go2do.deviceSync.business.device.module.DeviceRegisterBO;
import top.go2do.deviceSync.business.device.module.DeviceRegisterDTO;
import top.go2do.deviceSync.business.device.service.DeviceService;

/**
 * @Description
 * @Author xingyj
 * @CreateTime 2025/4/27 12:49
 */
@RequestMapping("/devices")
@RestController
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 设备注册接口
     * @param registerDTO
     */
    @PostMapping
    public void toRegister(@Valid @RequestBody DeviceRegisterDTO registerDTO) {
        DeviceRegisterBO registerBO = new DeviceRegisterBO();
        BeanUtils.copyProperties(registerDTO, registerBO);
        deviceService.register(registerBO);
    }

    /**
     * 设备心跳接口
     * @param heartbeatDTO
     * @return 心跳间隔时间（秒）
     */
    @PostMapping("/heartbeat")
    public Integer toHeartBeat(@Valid @RequestBody DeviceHeartbeatDTO heartbeatDTO) {
        DeviceHeartbeatBO heartbeatBO = new DeviceHeartbeatBO();
        BeanUtils.copyProperties(heartbeatDTO, heartbeatBO);
        return deviceService.heartbeat(heartbeatBO);
    }

}
